package com.fenshi.interactor.imp;

import android.content.Context;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.android.volley.Request;
import com.android.volley.VolleyError;
import com.fenshi.bean.OrderBean;
import com.fenshi.bean.RequestBean;
import com.fenshi.interactor.BackCarInteractor;
import com.fenshi.presenter.listener.RequestListener;
import com.fenshi.until.MethodCode;
import com.fenshi.until.MethodType;
import com.quickdv.helper.NetWorkImp;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by lilongfei on 15/10/13.
 */
public class BackCarInteractorImp extends NetWorkImp implements BackCarInteractor {

    protected RequestListener<OrderBean> listener;

    public BackCarInteractorImp(Context c) {
        super(c);
    }

    public BackCarInteractorImp(Context c, RequestListener<OrderBean> listener){
        this(c);
        this.listener = listener;
    }

    @Override
    public void backCar(String userId, String cardId) {
        RequestBean bean = new RequestBean();
        Map<String, Object> params = new HashMap<>();
        params.put("cardId", cardId);
        bean.setRequestParams(params);
        bean.setUserSession(userId);
        bean.setRequestCode(MethodType.CLOSE_THE_CAR);
        addFastJsonQueue(Request.Method.POST, MethodType.CLOSE_THE_CAR, (JSONObject) JSON.toJSON(bean));
        startRequest();
    }

    @Override
    public void checkCar(String userId, String type, String cardId) {
        RequestBean bean = new RequestBean();
        Map<String, Object> params = new HashMap<>();
        params.put("orderId", cardId);
        params.put("type",type);
        bean.setRequestParams(params);
        bean.setUserSession(userId);
        bean.setRequestCode(MethodType.VAILE_CAR);
        addFastJsonQueue(Request.Method.POST, MethodType.VAILE_CAR, (JSONObject) JSON.toJSON(bean));
        startRequest();
    }

    @Override
    protected void onRequestSuccess(Object tag, JSONObject result) {
        if (!isOk(result)) {
            listener.Error(tag + "", "返回数据格式有误");
            return;
        }
        if (tag.equals(MethodType.VAILE_CAR)){
            listener.Success(tag + "", null);
            return;
        }
        if (result == null){
            onRequestExcption(null);
            return;
        }
        JSONObject response = result.getJSONObject(MethodCode.responseContent_key);
        if (response == null) {
            onRequestExcption(null);
        } else {
            int  statue = response.getInteger(MethodCode.responseStatue);

            if (statue == MethodCode.NetState.SUCCESS.getValue()){
                if (tag.equals(MethodType.CLOSE_THE_CAR)) {
                    JSONObject obj = response.getJSONObject(MethodCode.OBJECT);
                    JSONObject order = obj.getJSONObject("carorder");
                    listener.Success(tag + "", JSON.toJavaObject(order, OrderBean.class));
                }
            }else //TODO 未完成
                listener.Error(tag + "", "还车失败,请联系管理员并检查车辆");
        }
    }

    @Override
    protected void onRequestExcption(VolleyError error) {
        listener.Exception("",error);
    }
}
